package com.gopay.solution.dao.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.solution.dao.SsAcctSolutionRelDAO;
import com.gopay.solution.domain.po.SsAcctSolutionRel;

/**
 * 账户与方案关系映射DAO实现
 * 
 * @SsAcctSolutionRelDAOImpl.java
 * @author MA HUAN
 * @2013-3-26 上午11:22:47 www.gopay.com.cn Inc.All rights reserved.
 */

@SuppressWarnings({ "unchecked", "rawtypes" })
@Repository(value = "ssAcctSolutionRelDAO")
public class SsAcctSolutionRelDAOImpl extends BaseDAOImpl<SsAcctSolutionRel, Serializable> implements
        SsAcctSolutionRelDAO {

    @Override
    public SsAcctSolutionRel getByAcctIdAndSoluType(String acctId, String solutionType) {

        String hql = " from SsAcctSolutionRel rel where rel.acctId = :AcctId and rel.solutionType = :SolutionType";

        Map param = new HashMap();
        param.put("AcctId", acctId);
        param.put("SolutionType", solutionType);

        List<SsAcctSolutionRel> res = super.findByHql(hql, param);

        if (res != null && res.size() > 0) {
            if (res.size() == 1)
                return res.get(0);

            // (acctId, solutionType) should be the Pk of this table, Otherwise
            // throw Exception.
            throw new IllegalStateException("More than One Record Showed Up!, acctId =" + acctId + " , solutionType = "
                    + solutionType);
        }

        return null;
    }
    
    public List<SsAcctSolutionRel> getByAcctIdAndSoluTypes(String acctId, String[] solutionTypes) {
    	
    	String hql = " from SsAcctSolutionRel rel "
    			+ "where rel.acctId = :AcctId "
    			+ "and rel.solutionType in (:SolutionTypes)" ;

        Map param = new HashMap();
        param.put("AcctId", acctId);
        param.put("SolutionTypes", solutionTypes);

        List<SsAcctSolutionRel> res = super.findByHql(hql, param);
        return res;
    }
    
    
    @Override
    public SsAcctSolutionRel getBySoluId(long solutionId){
        String hql = " from SsAcctSolutionRel rel where rel.solutionId = :solutionId ";

        Map param = new HashMap();
        param.put("solutionId", solutionId);

        List<SsAcctSolutionRel> res = super.findByHql(hql, param);

        if (res != null && res.size() > 0) {
            if (res.size() == 1)
                return res.get(0);
        }

        return null;
    }

}
